﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmkeniCrossLoginApplication.Data_Access;

namespace AmkeniCrossLoginApplication.Amkeni_System.CO
{
    public partial class ViewListofIPs : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void IPMetadataGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int pageSize = IPMetadataGridView.PageSize;
            int pageIndex = IPMetadataGridView.PageIndex;
            //Get the selected row
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            //GridViewRow row = EditGridView.Rows[currentRowIndex];
            int newRowIndex = 0;

            if (pageIndex > 0)
            {
                newRowIndex = pageIndex * pageSize;
                currentRowIndex = currentRowIndex - newRowIndex;
            }

            //Get the Datakeyname for the selected row
            //int selContract = Convert.ToInt64(this.EditGridView.DataKeys[currentRowIndex].Value);

            string downloadLocation = "~/Document_Upload/";

            var download = (from x in db.implementing_partner_metadata
                               where x.id == currentRowIndex
                               select x).Single();

            if (e.CommandName == "download_contract")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=Contract_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.contract));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_workplan")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=WorkPlan_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.workplan));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_budget")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=Budget_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.budget));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_logframe")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=LogFrame_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.logframe));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_regcert")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=RegistrationCertificate_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.registration_certificate));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_faceform")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=FaceForm_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.faceform));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_specimensignature")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=SpecimenSignature_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.specimen_signature));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
            else if (e.CommandName == "download_vendorform")
            {
                try
                {
                    Response.ClearContent();
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=VendorForm_" + download.implementing_partner.abbreviation);
                    Response.TransmitFile(Server.MapPath(downloadLocation + download.vendor_form));
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    throw new Exception("Error Fetching " + ex.Message);
                }
            }
        }

        protected void IPMetadataGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //make the controls visible or not from here
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // For the Contract
                LinkButton lnk_contract = (LinkButton)e.Row.FindControl("LnkDownloadContract");
                int id_contract = int.Parse(lnk_contract.CommandArgument.Trim());

                var contract = db.implementing_partner_metadata.Where(x => x.id == id_contract).Select(x => x.contract).Single();
                if (contract == string.Empty || contract == null)
                {
                    lnk_contract.Visible = false;
                }

                // For the Work Plan
                LinkButton lnk_wp = (LinkButton)e.Row.FindControl("LnkDownloadWorkplan");
                int id_wp = int.Parse(lnk_wp.CommandArgument.Trim());

                var wp = db.implementing_partner_metadata.Where(x => x.id == id_wp).Select(x => x.workplan).Single();
                if (wp == string.Empty || wp == null)
                {
                    lnk_wp.Visible = false;
                }

                // For the Budget
                LinkButton lnk_budget = (LinkButton)e.Row.FindControl("LnkDownloadBudget");
                int id_budget = int.Parse(lnk_budget.CommandArgument.Trim());

                var budget = db.implementing_partner_metadata.Where(x => x.id == id_budget).Select(x => x.budget).Single();
                if (budget == string.Empty || budget == null)
                {
                    lnk_budget.Visible = false;
                }

                // For the log frame
                LinkButton lnk_lf = (LinkButton)e.Row.FindControl("LnkDownloadLogframe");
                int id_lf = int.Parse(lnk_lf.CommandArgument.Trim());

                var lf = db.implementing_partner_metadata.Where(x => x.id == id_lf).Select(x => x.logframe).Single();
                if (lf == string.Empty || lf == null)
                {
                    lnk_lf.Visible = false;
                }

                // For the Reg Cert
                LinkButton lnk_cert = (LinkButton)e.Row.FindControl("LnkDownloadRegCert");
                int id_cert = int.Parse(lnk_cert.CommandArgument.Trim());

                var cert = db.implementing_partner_metadata.Where(x => x.id == id_cert).Select(x => x.registration_certificate).Single();
                if (cert == string.Empty || cert == null)
                {
                    lnk_cert.Visible = false;
                }

                // For the Face Form
                LinkButton lnk_ff = (LinkButton)e.Row.FindControl("LnkDownloadFaceForm");
                int id_ff = int.Parse(lnk_ff.CommandArgument.Trim());

                var ff = db.implementing_partner_metadata.Where(x => x.id == id_ff).Select(x => x.faceform).Single();
                if (ff == string.Empty || ff == null)
                {
                    lnk_ff.Visible = false;
                }

                // For the Specimen Signature
                LinkButton lnk_ss = (LinkButton)e.Row.FindControl("LnkDownloadSpecSignature");
                int id_ss = int.Parse(lnk_ss.CommandArgument.Trim());

                var ss = db.implementing_partner_metadata.Where(x => x.id == id_ss).Select(x => x.specimen_signature).Single();
                if (ss == string.Empty || ss == null)
                {
                    lnk_ss.Visible = false;
                }

                // For the Vendor Form
                LinkButton lnk_vf = (LinkButton)e.Row.FindControl("LnkDownloadVendorForm");
                int id_vf = int.Parse(lnk_vf.CommandArgument.Trim());

                var vf = db.implementing_partner_metadata.Where(x => x.id == id_vf).Select(x => x.vendor_form).Single();
                if (vf == string.Empty || vf == null)
                {
                    lnk_vf.Visible = false;
                }
            }
        }

        protected void UploadLinkButton_Click(object sender, EventArgs e)
        {
            //Do work
            LinkButton btn = (LinkButton)sender;
            int ipID = int.Parse(btn.CommandArgument.Trim());

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.id == ipID
                              select data).Single();

            //idLabel.Text = ipMetadata.id.ToString();
            //idNo = ipMetadata.id;

            Response.Redirect(String.Format("~/Amkeni_System/CO/Edit/AddIPMetadata.aspx?ipID={0}&rfpID={1}", ipMetadata.ip_id, ipMetadata.RFP_id));
            //Response.Redirect(String.Format("~/Amkeni_System/CO/Edit/AddIPMetadata.aspx?ipMetadata={0}", ipID));

            //mp1.Show();
        }
    }
}